<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2023 https://www.meoyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 喵云科技 【https://www.meoyun.com】
// +----------------------------------------------------------------------


namespace app\api\controller;

use app\common\service\WeChatMnpService;
use app\model\UsersModel;
use app\model\CommentModel;
use app\model\CommentThumbModel;
use app\model\PostModel;
use app\common\service\Message;

class Comment extends BaseApi
{
    protected ?string $dataModel = CommentModel::class;
    protected array $with = ['user', 'post'];
    protected array $allowSearch = ['content', 'status', 'user_id', 'post_id', 'pid'];

    //评论列表
    public function list()
    {
        $postId = input("post_id");
        $where["pid"] = "0";
        $uid = $this->getUserId();
        if (!empty($postId)) {
            $where["post_id"] = $postId;
        }

        $paginate = CommentModel::where($where)->with(['user'])->orderBy('created_at', 'desc')->paginate(10);

        $list = $paginate->items();

        foreach ($list as $item) {
            $item['is_thumb'] = false;

            if ($uid) {
                $item['is_thumb'] = CommentThumbModel::where(['user_id' => $uid, 'c_id' => $item['id']])->exists();
            }

            $children = CommentModel::with(["user", "replyUser"])->where("pid", $item["id"])->orderBy('thumb_num', 'desc')->first();

            if ($children) {
                $children["total"] = CommentModel::where("pid", $item["id"])->count();
                $item["children"] = $children;
            }
        }

        return $this->dataByPage($paginate);
    }

    //获取子级评论列表
    public function getChildrenList()
    {
        $parentId = input("parent_id");
        $fristId = input("frist_id");
        $uid = $this->getUserId();

        $where = [
            ["pid", "=", $parentId],
            ["id", "<>", $fristId] //排除第一条数据
        ];

        $paginate = CommentModel::with(["user", "replyUser"])->orderBy("thumb_num", "desc")->where($where)->paginate(10);
        $list = $paginate->items();

        foreach ($list as $item) {
            $item["is_thumb"] = CommentThumbModel::where(['user_id' => $uid, 'c_id' => $item['id']])->exists();
        }

        return $this->dataByPage($paginate);
    }

    //删除评论
    public function del()
    {
        $id = input("id");

        $commentInfo = CommentModel::where("id", $id)->first();
        $res = CommentModel::destroy($id);
        if ($res) {
            PostModel::where('id', $commentInfo['post_id'])->decrement('comment_num');
            return $this->success("删除成功");
        }
        return $this->fail("删除失败");
    }

    //发表评论
    public function addComment()
    {

        $userId = $this->getUserId();
        $params = request()->post();
        $params["user_id"] = $userId;

        $user = UsersModel::find($userId);

        if (!$user) {
            return $this->fail("登录用户不存在");
        }

        //检测账号是否可用
        if ($user->status === 0) {
            return $this->fail("您的账号异常，请联系客服。");
        }

        $checkText = (new WeChatMnpService())->msgSecCheck($user['wechat_mp_openid'], $params["content"]);

        if (!$checkText) {
            return $this->fail('内容包含敏感文字');
        }

        CommentModel::create($params);
        PostModel::where('id', $params["post_id"])->increment('comment_num');

        $post = PostModel::find($params["post_id"]);

        Message::send($userId, $post["user_id"], $params["post_id"], 2, $params["content"]);
        return $this->success('评论成功');
    }

    //评论点赞
    public function thumbAdd()
    {
        $id = input("id");

        $userId = $this->getUserId();

        $exists = CommentThumbModel::where('comment_id', $id)->where('user_id', $userId)->exists();
        if ($exists) {
            return $this->fail("请勿重复点赞");
        }

        $res = CommentThumbModel::create([
            'comment_id' => $id,
            'user_id' => $userId
        ]);

        if ($res) {
            $comment = CommentModel::find($id);
            Message::send($userId, $comment["user_id"], $comment["post_id"], 1, "赞了您的评论：" . $comment["content"]);
            return $this->success("点赞成功");
        }

        return $this->fail("点赞失败");
    }

    //取消点赞
    public function cancelThumb()
    {
        $id = input("id");
        $userId = $this->getUserId();

        $res = CommentThumbModel::where('comment_id', $id)->where('user_id', $userId)->delete();
        if ($res) {
            return $this->success("已取消点赞");
        }

        return $this->fail("取消失败");
    }
}